<?php

namespace app\modules\controllers;

use app\modules\models\Admin;
use app\modules\models\Contract;
use app\modules\models\Order;
use Yii;
use yii\data\Pagination;
use yii\db\Query;

class ContractController extends CommonController
{ 
    protected $mustlogin = ['list', 'add', 'mod', 'del'];

    public function actionList()
    {
        $this->layout = "layout1";
        $title=Yii::$app->request->get('title');
        $number = Yii::$app->request->get('number');
        $where_str='true ';
        if(isset($title)&&!empty($title)){
            $where_str.="and title like '%$title%' ";
        }
        if(isset($number)&&!empty($titler)){
            $where_str.="and title like '%$number%' ";
        }

        $model = Contract::find()->where($where_str)->orderBy('id DESC');
        $count = $model->where($where_str)->count();
        $pageSize=$this->pagesize('contractsize');
        $pager = new Pagination(['totalCount' => $count, 'pageSize' => $pageSize]);
        $data = $model->where($where_str)->offset($pager->offset)->limit($pager->limit)->all();
        $pageurl= $this->pageurl('contract/list');
        $adminlist = $this->getAdminList();
        $user = Yii::$app->admin->identity->adminuser;
        $bool=in_array($user,$adminlist);
        return $this->render("contracts", ['data' => $data, 'pager' => $pager,'total'=>$count,'pageurl'=>$pageurl,'pagesize' => $pageSize,'bool'=>$bool]);
    }

    public function actionAdd()
    {
        $model = new Contract();
        $this->layout = "layout1";
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->add($post)) {
                //Yii::$app->session->setFlash("info", "添加成功");
                $this->redirect(['contract/list']);
            }
        }
        return $this->render("add", ['model' => $model,'title'=>'添加合同']);
    }

    public function actionMod()
    {
        $this->layout = "layout1";
        $id = Yii::$app->request->get("id");
        $model = Contract::find()->where('id = :id', [':id' => $id])->one();
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->load($post) && $model->save()) {
                //Yii::$app->session->setFlash('info', '修改成功');
                $this->redirect(['contract/list']);
            }
        }
        return $this->render('add', ['model' => $model,'title'=>'修改合同']);
    }

    public function actionDel()
    {
        try {
            $id = Yii::$app->request->get('id');
            if (empty($id)) {
                throw new \Exception('参数错误');
            }
            $onearr=Contract::find()->where('id = :id', [":id" => $id])->one();
            $num = $onearr->number;
            $data = Order::find()->where('contractNum = :cnum', [":cnum" => $num])->one();
            if ($data) {
                throw new \Exception('该合同下有关联订单，不允许删除');
            }
            if (!Contract::deleteAll('id = :id', [":id" => $id])) {
                throw new \Exception('删除失败');
            }
            Yii::$app->session->setFlash('info', '删除成功');
        } catch(\Exception $e) {
            Yii::$app->session->setFlash('info', $e->getMessage());
        }
        return $this->redirect(Yii::$app->request->referrer);
    }



    private function getAdminList(){
        $auth = Yii::$app->authManager;
        $arr_admin=(new Query())->from($auth->assignmentTable)->where("item_name = 'master'")->all();
        $admin_model=new Admin();
        $adminlist=$admin_model->getAdminAll();
        $new_admin=[];
        foreach ($arr_admin as $k=>$v){
            $new_admin[$v['user_id']]=$adminlist[$v['user_id']];
        }
        return $new_admin;
    }



}